<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { financeApi } from '@/api/controller';
  import { ToastSuccess, Toastloading, allNumber } from '@/utils/public';
  import serachUserid from '@/components/serachUserid/index.vue';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    kk_balance:''
  })
  interface formQuert {
    uuid:number
    user_id:number
    kk_value:number
    desc:string
    msg:string
  }
  let form = reactive({} as formQuert)
  const formRules = {
    user_id: [{ required: true, message: "请输入用户UUID", trigger: "blur" }],
    kk_value: [{ required: true, message: "请输入发放金币数量", trigger: "blur" }],
    msg: [{ required: true, message: "请输入系统通知", trigger: "blur" }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate((valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      financeApi.createIncreaseKkValue(form).then(()=>{
        loading.close()
        ToastSuccess('发放成功')
        $emit('updateList')
        closeDialog()
      }).catch(()=>{
        loading.close()
      })
    })
  }
  const openDialog = ()=> {
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    action.kk_balance = ''
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="52%"
      title="发放用户金币">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="用户UUID" prop="user_id">
          <serachUserid
            v-model:uuid="form.uuid"
            v-model:user_id="form.user_id"
            v-model:kk_balance="action.kk_balance"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="用户金币余额">
          <el-input v-model="action.kk_balance" disabled />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="发放金币数量" prop="kk_value">
          <el-input v-model="form.kk_value" @input="allNumber(form,'kk_value')" placeholder="请输入发放金币数量" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="发放说明">
          <el-input
            v-model="form.desc"
            :autosize="{ minRows: 3, maxRows: 3}"
            type="textarea"
            placeholder="请输入发放说明"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="系统通知" prop="msg">
          <el-input
            v-model="form.msg"
            :autosize="{ minRows: 3, maxRows: 3}"
            type="textarea"
            placeholder="请输入系统通知"
          />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>